//
//  MTTHomeableViewCell.swift
//  MTWeiBo
//
//  Created by user on 2017/7/26.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class MTTHomeableViewCell: UITableViewCell {

    
    //记录toolbar的顶部约束
    var toolbarTopConstranitl: Constraint?
    
    //引用viwemodel
    var statusViewModel: MTSatusViewModel? {
        didSet{
            originalView.statusViewModel = statusViewModel
            //  注意点： 重新创建toolbar底部约束之前，把之前的顶部约束卸载掉，保证toolbar顶部只有一条约束
            toolbarTopConstranitl?.deactivate()
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博
                //  1. 显示转发微博视图
                retweetView.isHidden = false
                //  2. 重新创建toolBar顶部约束
                toolBar.snp.makeConstraints({ (make) in
                    //  重新记录新创建的toolbar顶部约束
                    toolbarTopConstranitl = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                //  3. 绑定转发微博内容
                retweetView.statusViewModel = statusViewModel
            } else {
                //  没有转发微博
                //  1. 隐藏转发微博视图
                retweetView.isHidden = true
                //  2. 重新创建toolBar顶部约束
                toolBar.snp.makeConstraints({ (make) in
                    //  重新记录新创建的toolbar顶部约束
                    toolbarTopConstranitl = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }

        }
    }
    
    
    //  MARK: --懒加载控件
    //  原创微博视图
    fileprivate lazy var originalView: MTHomeOriginalView = MTHomeOriginalView()
    // toolbar视图
    fileprivate lazy var toolBar: MTHomeToolBar = MTHomeToolBar()
    //转发视图
    fileprivate lazy var retweetView: MTHomeReweetView = MTHomeReweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
       // contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        //约束
        originalView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(20)
            make.leading.trailing.equalTo(contentView)
            //make.height.equalTo(100)
        }
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
            //make.height.equalTo(1100)
        }
        toolBar.snp.makeConstraints { (make) in
            toolbarTopConstranitl = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            
            //关键约束,确定行高
            make.bottom.equalTo(contentView)
        }
    }
    
}
